'use strict';
const dayjs = require("dayjs");
module.exports = app => {
    const { STRING, INTEGER, DATE } = app.Sequelize;
    const EnableHistory = app.model.define(
        'enableHistory',
        {
            id: {type: INTEGER, primaryKey: true, autoIncrement: true},
            device_id: INTEGER(11),
            member_id: INTEGER(11),
            connect_id: INTEGER(11),
            type: INTEGER(11),
            // 格式化时间
            updated_time: {
                type: DATE,
                get() {
                    return dayjs(this.getDataValue('updated_time')).format('YYYY-MM-DD HH:mm:ss');
                },
            },
            create_time: {
                type: DATE,
                get() {
                    return dayjs(this.getDataValue('create_time')).format('YYYY-MM-DD HH:mm:ss');
                },
            }
        },
        {
            tableName: 'enable_history',
            timestamps: true, // 启用时间戳(createdAt, updatedAt)
            updatedAt: 'updated_time', // 不想要 updatedAt
            createdAt: 'create_time', // 想要  createdAt 但是希望名称叫做 created_at
        }
    );
    // 关联
    EnableHistory.associate = function() {
        app.model.EnableHistory.belongsTo(app.model.Device, { foreignKey: 'device_id', targetKey: 'id' });
        app.model.EnableHistory.belongsTo(app.model.Member, { foreignKey: 'member_id', targetKey: 'id' });
    }
    return EnableHistory
};
